package go.mybatis.daoimpl;


import go.mybatis.dao.UserDao;
import go.mybatis.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.Date;
import java.util.List;


/**
 * Created by shehuilong on 2018/5/5.
 */

public class UserDaoImplTest {
    private UserDao userDao;

    @Before
    public void setUp() throws Exception {
        SqlSession sqlSession = null;

        String s = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(s);

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sqlSessionFactory.openSession();

        this.userDao = new UserDaoImpl(sqlSession);
    }

    @Test
    public void queryById() throws Exception {
        User user = userDao.queryById(1l);
        System.out.println(user);
    }

    @Test
    public void queryBy() throws Exception {
        List<User> users = userDao.queryBy();
        for (User user :users) {
            System.out.println(user);
        }
    }

    @Test
    public void insertUser() throws Exception {
        User user = new User();
        user.setUserName("kkk");
        user.setPassword("fff");
        user.setName("maxuelong");
        user.setAge(18);
        user.setSex(1);
        user.setBirthday(new Date());
        userDao.insertUser(user);

    }

    @Test
    public void updataUser() throws Exception {
        User user = userDao.queryById(21l);
        user.setName("金.卡戴珊");
        user.setAge(23);
        userDao.updataUser(user);

    }

    @Test
    public void deleteById() throws Exception {
        userDao.deleteById(28l);
    }


}
